// ++++++++++++++++++++++++++++++++++++++++
// 《Go 语言+Vue.js 实战派——基于 Gin 框架》源码
// ++++++++++++++++++++++++++++++++++++++++
// 作者公众号：源码大数据
// 自媒体账号（抖音、视频号、快手、B站、知乎）：廖显东-ShirDon
// 仓库地址：https://gitee.com/shirdonl/goGinVue.git
// 仓库地址：https://github.com/shirdonl/goGinVue.git
// // 博客：https://www.shirdon.com
// 购买图书：https: //item.jd.com/14958064.html
// ++++++++++++++++++++++++++++++++++++++++

package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type Customer struct {
	ID    int    `json:"id"`
	Name  string `json:"name"`
	Email string `json:"email"`
}

func main() {
	r := gin.Default()

	r.GET("/customers/:id", func(c *gin.Context) {
		param := c.Param("id")
		id, err := strconv.Atoi(param)
		if err != nil {
			// 处理错误
		}
		//从数据库或任何其他来源获取 Customer
		customer := Customer{ID: id, Name: "ShirDon", Email: "shirdonliao@example.com"}

		//将用户对象 customer 序列化为 JSON 格式
		jsonData, err := json.Marshal(customer)
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": "serialization error"})
			return
		}

		c.Writer.Header().Set("Content-Type", "application/json")
		c.Writer.Write(jsonData)
	})

	r.Run(":8080")
}
